# -*- encoding: utf-8 -*-
"""
@File  : logger.py
@Description : 日志系统
"""

# here put the import lib
import os
from datetime import datetime
import logging
from loguru import logger
from conf.settings import LOG_DIR


# class PropagateHandler(logging.Handler):
#     def emit(self, record):
#         logging.getLogger(record.name).handle(record)

class Log:
    """输出日志到文件和控制台"""

    def __init__(self):
        # 文件的命名
        log_name = f"test_{datetime.now().strftime('%Y_%m_%d_%H%M%S')}.log"
        log_path = os.path.join(LOG_DIR, log_name)
        # 判断日志文件夹是否存在，不存则创建
        if not os.path.exists(LOG_DIR):
            os.mkdir(LOG_DIR)
        # 日志输出格式
        formatter = "{time:YYYY-MM-DD HH:mm:ss} | {level}: {message}"
        # 日志写入文件,调用 logger.add() 方法来配置文件输出，保存到指定目录文件中
        logger.add(
            log_path,  # 写入目录指定文件
            format=formatter,
            encoding="utf-8",
            retention="2 days",  # 设置历史保留时长
           # serialize=True,#日志保存成json格式
            # rotation="500 MB",  # 切割，设置文件大小
            # compression="zip"   # 文件压缩
        )

    @staticmethod
    def debug(msg):
        logger.debug(msg)

    @staticmethod
    def info(msg):
        logger.info(msg)

    @staticmethod
    def warning(msg):
        logger.warning(msg)

    @staticmethod
    def error(msg):
        logger.error(msg)

    @staticmethod
    def exception(msg):
        logger.exception(msg)

    @staticmethod
    def trace(msg):
        logger.trace(msg)


log = Log()
